package cn.gorit.admin.service;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.exception.AdminException;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.admin.AdminDto;
import cn.gorit.dto.user.LoginDto;
import cn.gorit.entity.admin.Admin;
import cn.gorit.mapper.admin.AdminMapper;
import cn.gorit.utils.Page;
import cn.gorit.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @Classname AdminService
 * @Description 管理员相关的服务接口  back 代表后台相关的接口
 * @Author Gorit
 * @Created by 2021/3/8 23:53
 * @IDLE IntelliJ IDEA
 **/
@Service
public class AdminService {

    @Autowired
    private AdminMapper mapper;

    // 查询信息的接口
    public Result<Admin> getInfoById(Integer id) {
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),mapper.getById(id));
    }

    // 更新接口
    public Result<Object> updateInfo(AdminDto admin) {
        Integer res = mapper.update(admin);
        if (res > 0)
            return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg());
        return new Result<>(ResultEnums.UPDATE_FAILED.getCode(),ResultEnums.UPDATE_FAILED.getMsg());
    }

    // 添加
    public Result<Object> addAdmin(AdminDto admin) {
        mapper.add(admin);
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg());
    }

    public Result<Object> delAdminById(Integer id) {
        mapper.delById(id);
        return new Result<>(ResultEnums.DELETE_SUCCESS.getCode(),ResultEnums.DELETE_SUCCESS.getMsg());
    }

    // 登陆的接口
    public Result<Object> login(LoginDto dto) {
        Map<String,Object> map = new HashMap<>(1);
        AdminDto admin = mapper.getByUsername(dto.getUsername());
        if (admin == null) {
            throw new AdminException("你所输入的账户不存在");
        }
        // 登陆的逻辑编写
        if (admin.getPassword().equals(dto.getPassword())) {
            mapper.updateLoginTime(admin.getAdmin_id()); // 更新登录的时间
            admin.setUsername(dto.getUsername());
            admin.setPassword("你甭想知道");
            map.put("adminInfo",admin);
            return new Result<>(ResultEnums.LOGIN_SUCCESS.getCode(), ResultEnums.LOGIN_SUCCESS.getMsg(),map);
        }
        return new Result<>(ResultEnums.LOGIN_FAILED.getCode(),ResultEnums.LOGIN_FAILED.getMsg());
    }

    // 分页查询管理员列表
    public Result<Page<Admin>> getByPage(Page<Admin> page) {
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"name","login_time"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        List<Admin> list = mapper.getByPage(page);
        page.setList(list);
        int total = mapper.getCountByPage(page);
        page.setTotalCount(total);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }
}
